package com.hz.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hz.web.entity.QtUserEntity;
import com.hz.web.service.IqtUserService;
import com.hz.web.util.Base64;
import com.hz.web.util.Uid;

@Controller
@RequestMapping("qtuser")
public class QtUserController{

    @Resource
    private IqtUserService userService;

    /**
     * 查询
     * 
     * @return
     */
    @RequestMapping("queryUser")
    @ResponseBody
    public Map<String, Object> queryUser(HttpSession session, String username, String password) {
        Map<String, Object> resultMap = new HashMap<String, Object>();// 结果map
        Map<String, Object> parmsMap = new HashMap<String, Object>();// 参数map
        
        parmsMap.put("name", Base64.decode(username));
        parmsMap.put("pass", Base64.decode(password));

        QtUserEntity u = userService.queryUser(parmsMap);
        if (u != null) {
            resultMap.put("msg", "登陆成功！");
            session.setAttribute("qtuserinfo", u);
            return resultMap;
        }
        resultMap.put("msg", "登陆失败！");
        return resultMap;
    }

    /**
     * 注销
     * 
     * @return
     */
    @RequestMapping("removeSession")
    @ResponseBody
    public Object removeSession(HttpSession session) {

        if (session.getAttribute("qtuserinfo") != null) {
            session.removeAttribute("qtuserinfo");
        }
        return 200;
    }

    /**
     * 得到session中用户对象
     * 
     * @return
     */
    @RequestMapping("getSession")
    @ResponseBody
    public Map<String, Object> getSession(HttpSession session) {
        Map<String, Object> resultMap = new HashMap<String, Object>();// 结果map

        if (session.getAttribute("qtuserinfo") != null) {
            QtUserEntity u = (QtUserEntity) session.getAttribute("qtuserinfo");
            resultMap.put("zt", "0");
            resultMap.put("username", u.getUsername());
        } else {
            resultMap.put("zt", "1");
        }

        return resultMap;
    }

    /**
     * 新增用户
     * 
     * @return
     */
    @RequestMapping("addUser")
    @ResponseBody
    public Map<String, Object> addUser(HttpSession session, String yhm, String password1) {
    	yhm = Base64.decode(yhm);
    	password1=Base64.decode(password1);
        Map<String, Object> resultMap = new HashMap<String, Object>();// 结果map
        Map<String, Object> parmsMap = new HashMap<String, Object>();// 参数map

        parmsMap.put("id", Uid.getUid());
        parmsMap.put("name", yhm);
        parmsMap.put("pass",password1 );
        int row = 0;

        if (yhm != null) {
            yhm = yhm.replaceAll(" ", "");
            Map<String, Object> parmsMap1 = new HashMap<String, Object>();// 参数map
            parmsMap1.put("name", yhm);
            QtUserEntity qtUser = userService.queryUser(parmsMap1);
            if (qtUser != null) {
                resultMap.put("msg", "登录帐户已存在！");
                return resultMap;
            }
        } else {
            resultMap.put("msg", "登录帐户为必填字段！");
            return resultMap;
        }

        try {
            row = userService.addUser(parmsMap);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("msg", "添加异常");
            return resultMap;
        }
        resultMap.put("msg", "添加成功！");

        return resultMap;
    }

}
